const router = require('koa-router')();
const authority = require('./authority');
const user = require('./user');
const classification = require('./classification');
const article = require('./article');
const focus = require('./focus');
const url = require('url');

/**
 * admin主入口
 */
router.prefix('/admin');

/**
 * admin路由的自定义中间件
 */
router.use(async (ctx, next) => {
  // 左侧菜单列表项
  const sidebarMenu = [
    {icon: 'fa fa-user-circle-o', title: '用户管理', path: 'user',
      items: [
        {title: '用户列表', url: `${ctx.state.__ROOT__}/admin/user/index`, path: 'index'},
        {title: '增加用户', url: `${ctx.state.__ROOT__}/admin/user/add`, path: 'add'}
      ]
    },
    {icon: 'fa fa-th-large', title: '分类管理', path: 'classification',
      items: [
        {title: '分类列表', url: `${ctx.state.__ROOT__}/admin/classification/index`, path: 'index'},
        {title: '增加分类', url: `${ctx.state.__ROOT__}/admin/classification/add`, path: 'add'}
      ]
    },
    {icon: 'fa fa-pencil-square', title: '内容管理', path: 'article',
      items: [
        {title: '内容列表', url: `${ctx.state.__ROOT__}/admin/article/index`, path: 'index'},
        {title: '新增内容', url: `${ctx.state.__ROOT__}/admin/article/add`, path: 'add'}
      ]
    },
    {icon: 'fa fa-picture-o', title: '轮播图', path: 'focus',
      items: [
        {title: '图片列表', url: `${ctx.state.__ROOT__}/admin/focus/index`, path: 'index'},
        {title: '新增图片', url: `${ctx.state.__ROOT__}/admin/focus/add`, path: 'add'}
      ]
    },
    {icon: 'fa fa-linkedin-square', title: '友情链接', path: 'focus',
      items: [
        {title: '链接列表', url: `${ctx.state.__ROOT__}/admin/focus/index`, path: 'index'},
        {title: '新增链接', url: `${ctx.state.__ROOT__}/admin/focus/add`, path: 'add'}
      ]
    },
    {icon: 'fa fa-list', title: '导航管理', path: 'focus',
      items: [
        {title: '导航列表', url: `${ctx.state.__ROOT__}/admin/focus/index`, path: 'index'},
        {title: '新增导航', url: `${ctx.state.__ROOT__}/admin/focus/add`, path: 'add'}
      ]
    }
  ];
  // 解析当前路由
  const path = url.parse(ctx.url).pathname;
  ctx.state.G = {
    sidebarMenu,
    currPath: path.split('/')
  };
  if (ctx.session.userInfo) { // 用户已登录，放行
    ctx.state.G = {
      ...ctx.state.G,
      userInfo: ctx.session.userInfo
    };
    await next()
  } else { // 尚未登录
    // console.log(path);
    if (path === '/admin/login' || path === '/admin/doLogin' || path === '/admin/registry' || path === '/admin/doRegistry' || path === '/admin/code') { // 注册登录相关请求，放行
      await next()
    } else { // 非注册登录请求，跳转登录页
      ctx.redirect('/admin/login')
    }
  }
});

/**
 * 后台欢迎页
 */
router.get('/', ctx => {
  ctx.render('admin/index');
});

/**
 * 更改状态接口
 */
router.post('/changeStatus', async ctx => {
  const params = ctx.request.body;
  const filter = {_id: ctx.state.DB.getObjId(params.id)};
  // 查询id对应的数据
  const data = await ctx.state.DB.find(params.col, filter);
  let body = {};
  if (data.length > 0) {
    try {
      const result = await ctx.state.DB.update(params.col, filter, {[params.field]: !data[0][params.field]});
      if (result && result.ok) {
        body = {code: 1, msg: '更新状态成功'}
      }
    } catch (e) {
      body = {code: -1, msg: '更新状态失败'};
    }
  } else {
    body = {code: -1, msg: '更新的数据不存在'};
  }
  ctx.body = body;
});

/**
 * 更改状态接口
 */
router.post('/deleteDoc', async ctx => {
  const params = ctx.request.body;
  const filter = {_id: ctx.state.DB.getObjId(params.id)};
  let body = {};
  try {
    const result = await ctx.state.DB.remove(params.col, filter);
    if (result && result.ok) {
      body = {code: 1, msg: '删除成功'}
    }
  } catch (e) {
    body = {code: -1, msg: '删除失败'};
  }
  ctx.body = body;
});

router.use(authority.routes());
router.use(user.routes());
router.use(classification.routes());
router.use(article.routes());
router.use(focus.routes());


module.exports = router;